/*
 * @Author: your name
 * @Date: 2021-12-22 16:01:12
 * @LastEditTime: 2021-12-22 20:42:23
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \day11  ts\app\src\store\index.ts
 */

import {createStore,applyMiddleware} from "redux"
import thunk from "redux-thunk"
import logger from "redux-logger"

import {State,Action} from "../types/index.d"

const reducer=(state:State={list:[],text:""},action:Action)=>{
    switch (action.type) {
        case "GETLIST":
            state.list=action.list
            return {
                ...state,
                list:[...state.list]
            }
        case "ADDLIST":
            state.list.push(action.obj)
            return {
                ...state,
                list:[...state.list]
            }
        case "REPLAYLIST":
            console.log(action);
            state.list.forEach(item=>{
                if(item.id===action.id){
                    item.child&&item.child.push({
                        name:"我想回家了",
                        context:action.comment,
                        id:new Date().getTime()+""
                    })
                }
            })
            return {
                ...state,
                list:[...state.list]
            }
        default:
            return {
                ...state
            }
    }
}

const store=createStore(reducer,applyMiddleware(thunk,logger))

export default store